package com.egg.ylt.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.adapter.ADA_DeviceRecyclerAdapter;
import com.egg.ylt.pojo.GetEquipmentsEntity;
import com.egg.ylt.presenter.impl.DevicePresenterImpl;
import com.egg.ylt.view.IDeviceView;
import com.google.gson.Gson;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_DeviceManagement extends BaseActivity<DevicePresenterImpl> implements IDeviceView {

    @BindView(R.id.layout_top)
    RelativeLayout mLayoutTop;
    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_select)
    TextView includeTvSelect;
    @BindView(R.id.include_tv_delete)
    TextView includeTvDelete;
    @BindView(R.id.include_tv_cancel)
    TextView includeTvCancel;
    @BindView(R.id.device_recycler_view)
    RecyclerView deviceRecyclerView;
    @BindView(R.id.ll_loading_view)
    LinearLayout llLoadingView;
    private ADA_DeviceRecyclerAdapter ada_recycler;
    private String pageSize = "50";
    private String pageNum = "1";
    private boolean listSize = false;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_device_management;
    }

    @Override
    protected View getLoadingTargetView() {
        return llLoadingView;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mLayoutTop);

        //设置recycle布局管理器和适配器
        deviceRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        ada_recycler = new ADA_DeviceRecyclerAdapter(mContext);
        deviceRecyclerView.setAdapter(ada_recycler);

        //请求设备数据
        mPresenter.postDeviceData(Constants.TOKEN, Constants.COMPANYID, pageSize, pageNum);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getDeviceData(GetEquipmentsEntity equipmentsEntity) {
        if (equipmentsEntity == null) {
            return;
        }
        if (ListUtil.isListEmpty(equipmentsEntity.getList())) {
            listSize = true;
            toggleShowEmpty(true, R.drawable.empty_no_response, "空空如也~");
            return;
        }

        toggleShowEmpty(false, -1, "");
        //加载设备数据
        listSize = false;
        ada_recycler.setmList(equipmentsEntity.getList());
    }

    @Override
    public void deleteDeviceSucceed() {
        //服务器删除成功后，执行本地删除方法
        //请求设备数据
        mPresenter.postDeviceData(Constants.TOKEN, Constants.COMPANYID, pageSize, pageNum);
        ada_recycler.setType(false);
        includeTvSelect.setVisibility(View.VISIBLE);
        includeTvDelete.setVisibility(View.GONE);
        includeTvCancel.setVisibility(View.GONE);
    }

    @OnClick({R.id.include_iv_back, R.id.include_tv_select, R.id.include_tv_delete, R.id.include_tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.include_tv_select://选择
                if (listSize) {
                    return;
                }
                includeTvSelect.setVisibility(View.GONE);
                includeTvDelete.setVisibility(View.VISIBLE);
                includeTvCancel.setVisibility(View.VISIBLE);
                ada_recycler.setType(true);
                break;
            case R.id.include_tv_delete://删除
                //根据选中的设备进行删除
                deleteDevice();
                break;
            case R.id.include_tv_cancel://取消
                includeTvSelect.setVisibility(View.VISIBLE);
                includeTvDelete.setVisibility(View.GONE);
                includeTvCancel.setVisibility(View.GONE);
                ada_recycler.setType(false);
                break;
        }
    }

    /**
     * 删除设备
     */
    private void deleteDevice() {
        //当前设备数据集合
        List<GetEquipmentsEntity.ListBean> listBeans = ada_recycler.getmList();
        //待删除的集合
        List<String> idsList = new ArrayList<>();
        //循环数据
        for (int i = 0, lenth = listBeans.size(); i < lenth; i++) {
            //当前设备对象
            GetEquipmentsEntity.ListBean dataBean = listBeans.get(i);
            //isSelect = true  就是要删除的item
            if (dataBean.isSelected()) {
                //把要删除的item  设备ID传入deleteBean中
                idsList.add(dataBean.getId());
            }
        }
        if (idsList.size() == 0) {
            ToastUtil.makeText(mContext, "请选择设备", false);
            return;
        }
        //生成带删除设备的字符串
        Gson gson = new Gson();
        String deleteJson = gson.toJson(idsList);

        //执行删除操作
        mPresenter.deleteDevice(Constants.TOKEN, deleteJson);
    }
}
